package com.instagram.instadownloader.history;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.MimeTypeMap;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.vine.vinedownloader.R;
import com.instagram.instadownloader.utils.UniversalImageLoader;

/**
 * The Class HistoryAdapter.
 * @author HuanND
 */
public class HistoryAdapter extends ArrayAdapter<HistoryObject>{

	/** The m list data. */
	private ArrayList<HistoryObject> mListData;
	
	/** The m context. */
	private Context mContext;
	
	/** The m listener. */
	private OnHistoryItemClickListener mListener;
	
	/**
	 * Instantiates a new history adapter.
	 *
	 * @param context the context
	 * @param historyList the history list
	 * @param listener the listener
	 */
	public HistoryAdapter(Context context, ArrayList<HistoryObject> historyList,OnHistoryItemClickListener listener) {
		super(context, R.layout.view_listview_row,historyList);
		mContext = context;
		mListData = historyList;
		mListener = listener;
	}
	
	public void setDatas(List<HistoryObject> datas) {
		mListData = (ArrayList<HistoryObject>) datas;
		this.notifyDataSetChanged();
	}

	/* (non-Javadoc)
	 * @see android.widget.ArrayAdapter#getItem(int)
	 */
	@Override
	public HistoryObject getItem(int position) {
		return mListData.get(position);
	}
	
	/* (non-Javadoc)
	 * @see android.widget.ArrayAdapter#getView(int, android.view.View, android.view.ViewGroup)
	 */
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View row = convertView;
		ViewHolder viewHolder;
		if(row == null){
			row = LayoutInflater.from(mContext).inflate(R.layout.view_listview_row, null);
			viewHolder = new ViewHolder();
			viewHolder.image = (ImageView)row.findViewById(R.id.ivIcon);
			viewHolder.options = (ImageView)row.findViewById(R.id.ivMore);
			viewHolder.share = (ImageView)row.findViewById(R.id.ivShare);
			viewHolder.text = (TextView)row.findViewById(R.id.tvLabel);
			row.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder) convertView.getTag();
		}
		HistoryObject object = mListData.get(position);
		
		String absolutePath = object.getAbsolutePath();
		if(absolutePath!=null){
			viewHolder.text.setText(absolutePath);
		}
		
		String extension = MimeTypeMap.getFileExtensionFromUrl(absolutePath);
		String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
		if(mimeType!=null){
			if(mimeType.contains("image")){
				try {
//					Bitmap localBitmap = MediaStore.Images.Media.getBitmap(this.mContext.getContentResolver(), Uri.parse("file://" + absolutePath));
//					viewHolder.image.setImageBitmap(localBitmap);
					if(object.getUrlImage()!=null && !object.getUrlImage().isEmpty()){
						UniversalImageLoader.getInstance().displayImage(object.getUrlImage(), viewHolder.image);
					}
					if(object.getUrlImage().isEmpty()){
						UniversalImageLoader.getInstance().displayImage("file://" + absolutePath, viewHolder.image);
					}
				} catch (Exception e) {
					e.printStackTrace();
					viewHolder.image.setImageResource(R.drawable.ic_image_file);
				}
			}else if(mimeType.contains("video")){
				viewHolder.image.setImageResource(R.drawable.ic_video_file);
			}else{
				viewHolder.image.setImageResource(R.drawable.ic_image_file);
			}
		}
		
		viewHolder.share.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(mListener!=null){
					mListener.onShareClick(position);
				}
			}
		});
		
		viewHolder.options.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(mListener!=null){
					mListener.onOptionClick(position);
				}
			}
		});
		
		return row;
	}
	
	/**
	 * The Class ViewHolder.
	 */
	static class ViewHolder
	  {
	    
    	/** The image. */
    	public ImageView image;
	    
    	/** The options. */
    	public ImageView options;
	    
    	/** The share. */
    	public ImageView share;
	    
    	/** The text. */
    	public TextView text;
	  }
	
	/**
	 * The listener interface for receiving onHistoryItemClick events.
	 * The class that is interested in processing a onHistoryItemClick
	 * event implements this interface, and the object created
	 * with that class is registered with a component using the
	 * component's <code>addOnHistoryItemClickListener<code> method. When
	 * the onHistoryItemClick event occurs, that object's appropriate
	 * method is invoked.
	 *
	 * @see OnHistoryItemClickEvent
	 */
	public interface OnHistoryItemClickListener{
		
		/**
		 * On option click.
		 *
		 * @param pos the pos
		 */
		public void onOptionClick(int pos);
		
		/**
		 * On share click.
		 *
		 * @param pos the pos
		 */
		public void onShareClick(int pos);
	}
}
